/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.messaging.inbox;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import nl.cloudfarming.client.messaging.Message;
import nl.cloudfarming.client.messaging.MessageEvent;
import nl.cloudfarming.client.messaging.MessageEventKey;
import nl.cloudfarming.client.messaging.MessageEventProducer;
import nl.cloudfarming.client.messaging.MessageModule;
import org.openide.util.NbBundle;

/**
 * A popup window (JFrame) to display a single Message
 * 
 * @author Gerben Feenstra
 */
public class MessageDisplayFrame extends JFrame {

    /**
     * Create a window to display the specified message
     * @param message The Message to display in this window
     */
    public MessageDisplayFrame(Message message){

        // Create frametitle
        StringBuilder titleSb = new StringBuilder();
        
        
        titleSb.append(new SimpleDateFormat(NbBundle.getMessage(MessageModule.class, "messaging.message.datereceived.format")).format(message.getDateReceived()));
        titleSb.append(" - ");
        titleSb.append(message.getTitle());         
        
        // Set frame
        this.setTitle(titleSb.toString());       
        this.setResizable(true);
        this.setPreferredSize(new Dimension(600, 400));
        this.setLayout(new BorderLayout());

        // Get message display panel
        this.add(new MessageDisplayPropertyPanel(message), BorderLayout.NORTH);
        this.add(new MessageDisplayContentPanel(message, NbBundle.getMessage(MessageModule.class, "messaging.messagepopup.label.content.text") + ":"), BorderLayout.CENTER);

        this.pack();
        
        // Center the popup
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        this.setLocation((int) (d.getWidth() - this.getWidth()) / 2, (int) (d.getHeight() - this.getHeight()) / 2);

        // Activate frame
        this.setVisible(true);
              
        // Trigger event
        new MessageEventProducer().triggerEvent(new MessageEvent(message, MessageEventKey.MESSAGE_READ)); 
        
        // Set message as read
        message.setRead(true);
    }


    
}


